(function ($) {
    window.URL = location.origin+'/htmll/'
    function addbar() {
        var bar_data = {
            bar: [
                {
                    class: 'index',
                    name: '家',
                    url: URL + 'index.html'
                },
                {
                    class: 'class',
                    name: '分类',
                    url: URL + 'class.html'
                },
                // {
                //     class: 'find',
                //     name: '资讯中心',
                //     url: URL + 'find.html'
                // },
                {
                    class: 'cart',
                    name: '购物车',
                    url: URL + 'cart.html'
                },
                {
                    class: 'my',
                    name: '我的',
                    url: URL + 'my.html'
                }
            ]
        };

        var html = '<div class="subnav">'
        html += '<a v-for="(item,index) in bar"  :href="item.url" :class="[item.class,now==item.class?\'cur\':\'\']">'
        html += '<span class="tb"></span>'
        html += '<span class="tt">{{item.name}}</span>'
        html += '</a>'
        html += '</div>'

        Vue.component('bar', {
            template: html,
            props: ['now'],
            data: function () {
                return bar_data;
            }
        })
    }

    addbar()

    //登录相关
    $.toSignin = function (back) {
       
        var from = back || (location.pathname + location.search);
		
        if ($.is_weixin()) {
            window.location.replace('/WeiXin/Login' + (from ? ('?backUrl=' + from) : ''));
        } else {

            window.location.replace(window.URL + 'signin.html' + (from ? ('?from=' + $.base64encode(from)) : ''))
        }
    }

    $.putUser = function (rs) {
        localStorage['yes'] = $.base64encode(JSON.stringify(rs))
    }
    $.getUser = function (rs) {
        return rs ? (localStorage['yes'] ? JSON.parse(plus.base64decode(localStorage['yes']))[rs] : '') : (localStorage['yes'] ? JSON.parse(plus.base64decode(localStorage['yes'])) : '')
    }
    $.clearUser = function () {
        localStorage.clear()
    }
    $.editUser = function (res, val) {
        var data = JSON.parse($.base64decode(localStorage['yes']))
        data[res] = val
        $.putUser(data)
    }

    $.putToken = function (res) {
        localStorage['yesToken'] = $.base64encode(res.UserName + ':' + res.DynamicToken);
    }

    $.checkUser = function () {

        if ($.is_weixin()) {
            var user = Cookies.getJSON('userInfo')?decodeURIComponent(Cookies.getJSON('userInfo')):''
            if (user) {
                user = JSON.parse($.base64decode(user))
                $.putUser(user)
                $.putToken(user)
                Cookies.remove('userInfo', {path: '/'});
            }

        }

        window.TOKEN = localStorage['yesToken']
        if (window.TOKEN) {
            $.ajaxSetup({
                headers: {
                    Authorization: 'Basic ' + window.TOKEN
                }
            })
        } else {
            $.toSignin()
        }
    }

    $.CountDown = function (obj) {
        var t = 60;
        var timer = setInterval(function () {
            if (t == 0) {
                obj.html('获取验证码');
                obj.removeClass('on')
                clearInterval(timer);
            } else {
                obj.html(t + '秒后重发');
                t--;
            }

        }, 1000)
    }

    $(document).ajaxSuccess(function (event, xhr, settings) {

        if (xhr.responseText) {
            var res = JSON.parse(xhr.responseText)
            if (res.returnCode == '401') {
                $.toSignin()
            }

            if (res.returnCode != '200') {
                $.oppo(res.msg)
            }

        }

    });


})(jQuery)
